package com.quan.server;

import com.mark.util.JsonUtil;
import com.quan.server.beans.Customer;
import com.quan.server.beans.Income;
import com.quan.server.beans.TaobaoOrder;
import com.quan.server.helper.AlimamaClient;
import com.quan.server.helper.MyAppHelper;
import com.quan.server.jpa.CustomerRepository;
import com.quan.server.jpa.IncomeRepository;
import org.glassfish.jersey.internal.inject.Custom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import java.util.List;

@SpringBootApplication
public class ServerApplication {
    private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private MyAppHelper myAppHelper;

    @Autowired
    private IncomeRepository incomeRepository;

    @Value("${spring.profiles.active}")
    public String profileActive;

    @Autowired
    public AlimamaClient alimamaClient;

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

    @PostConstruct
    public void init() {
        //insert test customer
        initAlphaData();
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动file.encoding:" + System.getProperty("file.encoding"));
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        for (String key : System.getProperties().stringPropertyNames()) {
            logger.error("-------------------->>>>启动 {}: {}", key, System.getProperty(key));
        }

        List<Customer> customerList = customerRepository.findAll();
        for (Customer customer : customerList) {
            logger.error("------------>>>>customer:" + JsonUtil.bean2Json(customer));
            if (customer.alimamaPid != null && customer.alimamaId == null) {
                String[] tokens = customer.alimamaPid.split("_");
                if (tokens.length == 4) {
                    String alimamaId = tokens[1];
                    String alimamaSiteId = tokens[2];
                    String alimamaAdzoneId = tokens[3];
                    customer.alimamaId = alimamaId;
                    customer.alimamaSiteId = alimamaSiteId;
                    customer.alimamaAdzoneId = alimamaAdzoneId;
                    customerRepository.save(customer);
                }
//                System.out.println(customer);
            }
        }
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
        logger.error("-------------------->>>>启动");
    }

    private void initAlphaData() {
        if (!"alpha".equals(profileActive)) {
            return;
        }
        try {
            Client client = ClientBuilder.newClient();
            Response response = client.target("http://youhuiquan.duapp.com/api/customers/getIfCustomerIdByWeixinId")
                .queryParam("weixinId", "macmatilda")
                .request()
                .get();

            String customerId = response.readEntity(String.class);
            response = client.target("http://youhuiquan.duapp.com/api/customers/" + customerId)
                .request()
                .get();
            Customer customer = response.readEntity(Customer.class);

            customerRepository.save(customer);

            Income income = myAppHelper.getIfIncomeByCustomerId(customer.id);
            Income income1 = new Income();
            income1.id = income.id;
            income1.customerId = income.customerId;
            income1.moneyRest = 10;
            incomeRepository.save(income1);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}
